package cn.phecda.boot.common.sensitive;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.DesensitizedUtil;

import java.nio.charset.StandardCharsets;
import java.util.function.Function;

/**
 * <p> 脱敏实现 </p>
 *
 * @author : wanpinwei
 */
public interface SensitiveFunctions {
    ChineseName CHINESE_NAME = new ChineseName();
    IdCardNum ID_CARD_NUM = new IdCardNum();
    FixPhone FIX_PHONE = new FixPhone();
    MobilePhone MOBILE_PHONE = new MobilePhone();
    Address ADDRESS = new Address();
    Email EMAIL = new Email();
    Password PASSWORD = new Password();
    CarLicense CAR_LICENSE = new CarLicense();
    BankCard BACK_CARD = new BankCard();
    Ipv4 IPV4 = new Ipv4();
    Ipv6 IPV6 = new Ipv6();
    FirstMask FIRST_MASK = new FirstMask();
    Base64 BASE64 = new Base64();
    Empty EMPTY = new Empty();


    class ChineseName implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.chineseName(wrapper.getFieldValue());
        }
    }

    class IdCardNum implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.idCardNum(wrapper.getFieldValue(), 1, 2);
        }
    }
    class FixPhone implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.fixedPhone(wrapper.getFieldValue());
        }
    }
    class MobilePhone implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.mobilePhone(wrapper.getFieldValue());
        }
    }
    class Address implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.address(wrapper.getFieldValue(), 8);
        }
    }
    class Email implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.email(wrapper.getFieldValue());
        }
    }
    class Password implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.password(wrapper.getFieldValue());
        }
    }
    class CarLicense implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.carLicense(wrapper.getFieldValue());
        }
    }
    class BankCard implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.bankCard(wrapper.getFieldValue());
        }
    }
    class Ipv4 implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.ipv4(wrapper.getFieldValue());
        }
    }
    class Ipv6 implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.ipv6(wrapper.getFieldValue());
        }
    }

    class FirstMask implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return DesensitizedUtil.firstMask(wrapper.getFieldValue());
        }
    }

    class Base64 implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return java.util.Base64.getEncoder().withoutPadding().encodeToString(wrapper.getFieldValue().getBytes(StandardCharsets.UTF_8));
        }
    }

    class Empty implements Function<SensitiveMetaDataWrapper, String> {
        @Override
        public String apply(SensitiveMetaDataWrapper wrapper) {
            return CharSequenceUtil.EMPTY;
        }
    }
}
